// player.js
// 从最新 Three.js 的 jsm 模块中导入
import * as THREE from 'https://unpkg.com/three@0.165.0/build/three.module.js';
import { GLTFLoader } from 'https://unpkg.com/three@0.165.0/examples/jsm/loaders/GLTFLoader.js';

// 场景
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x0a1a2a);

// 相机
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(0, 2, 5);

// 渲染器
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

// 光源
const ambientLight = new THREE.AmbientLight(0x404040);
scene.add(ambientLight);

const directionalLight = new THREE.DirectionalLight(0xffffff, 1);
directionalLight.position.set(1, 1, 1).normalize();
scene.add(directionalLight);

// 加载器
const loader = new GLTFLoader();
loader.load(
    'worlds/fangcundian.daomo',
    (gltf) => {
        scene.add(gltf.scene);
        console.log('🎉 道境已显现：mochen_cave.daomo');
    },
    (progress) => {
        console.log(`加载中... ${Math.round(progress.loaded / progress.total * 100)}%`);
    },
    (error) => {
        console.error('❌ 道境封印未解', error);
    }
);

// 动画
function animate() {
    requestAnimationFrame(animate);
    renderer.render(scene, camera);
}
animate();

// 自适应
window.addEventListener('resize', () => {
    camera.aspect = window.innerWidth / window.innerHeight;
    camera.updateProjectionMatrix();
    renderer.setSize(window.innerWidth, window.innerHeight);
});

console.log('万仙境 · 播放器启动');